package com.tuanzi.loan.business.service.sms.el;

import org.apache.commons.lang3.reflect.MethodUtils;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public abstract class MatchRulePropertyAccessorImpl implements PropertyAccessor {

	@Override
	public boolean canRead(final EvaluationContext context, final Object target, final String name) throws AccessException {
		return true;
	}

	@Override
	public boolean canWrite(final EvaluationContext context, final Object target, final String name) throws AccessException {
		return false;
	}

	@Override
	public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException {
		return new TypedValue(doRead(target, name));
	}

	@Override
	public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
	}

	protected Object doRead(Object target, String name) {
		try {
			return MethodUtils.invokeExactMethod(target, name);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException(e);
		}
	}

}
